/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.portalnahouby.daoimplementation;

import cz.muni.fi.pa165.portalnahouby.daointerface.PickerDAO;
import cz.muni.fi.pa165.portalnahouby.entity.Picker;
import java.util.List;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ejb.Stateless;

/**
 * Implementation of PickerDAO interface
 *
 * @author Lukas Marica
 */
@Stateless
@LocalBean
public class PickerDAOImpl implements PickerDAO {

    @PersistenceContext
    private EntityManager em;

    @Override
    public void addPicker(Picker picker) {
        if (picker == null) {
            throw new IllegalArgumentException("Picker cannot be null");
        }
        if (picker.getId() != null) {
            throw new IllegalArgumentException("New picker has to have null ID");
        }

        em.persist(picker);
    }

    @Override
    public void editPicker(Picker picker) {
        if (picker == null) {
            throw new IllegalArgumentException("Picker cannot be null");
        }
        if (picker.getId() == null) {
            throw new IllegalArgumentException("Picker's ID cannot be null");
        }

        em.merge(picker);
    }

    @Override
    public void deletePicker(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("ID cannot be null");
        }

        Picker picker = em.find(Picker.class, id);

        em.remove(picker);
    }

    @Override
    public Picker findPicker(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("ID cannot be null");
        }

        return em.find(Picker.class, id);
    }

    @Override
    public List<Picker> getAllPickers() {
        return em.createQuery("SELECT p FROM Picker p", Picker.class).getResultList();
    }
}
